$(function() {
    $("#nav_worker").parent().addClass("active");

    // 样例
	$(".glyphicon-question-sign").each(function(){
		new Tippy('#'+$(this).attr("id"), {
			  html: '#'+$(this).next().attr("id"),
			  arrow: true,
			  animation: 'fade',
			  trigger:'click',
			  position:'top',
			  theme:'light',
			  shown: function() {
				  $(".tippy-tooltip").css("visibility","visible");
			  }
		});
	});
    
    // 实例化一个校验类，id选择器
    var checkout1 = new AKCheckout();
    checkout1.addField({id:'staffName', isRequired:true, emptyMsg:$.i18n.prop('check_full_name')});
    checkout1.addField({id:'department', isRequired:true, emptyMsg:$.i18n.prop('check_department')});
    checkout1.addField({id:'position', isRequired:true, emptyMsg:$.i18n.prop('check_position')});
    checkout1.addField({id:'entrydate', isRequired:true, emptyMsg:$.i18n.prop('check_entrydate')});
    checkout1.addField({id:'education', isRequired:true, emptyMsg: $.i18n.prop('check_education')});
    checkout1.addField({id:'gradDate', isRequired:true, emptyMsg: $.i18n.prop('check_grad_date')});
    checkout1.addField({id:'zzDate', isRequired:true, emptyMsg: $.i18n.prop('check_zz_date')});
    checkout1.addField({id:'effort', isRequired:true, emptyMsg: $.i18n.prop('check_zz_effort')});
    checkout1.addField({id:'gain', isRequired:true, emptyMsg: $.i18n.prop('check_zz_gain')});
    checkout1.addField({id:'suggest', isRequired:true, emptyMsg: $.i18n.prop('check_zz_suggest')});
    
    // 给文本输入框设置默认值
    initText();

    // 初始化日历插件
    // 设置入职日期
    $("#entryDatePicker").datetimepicker({
        format: "YYYY-MM-DD",
        locale: moment.locale(__lang),
        showTodayButton:true,
        showClear:true
    });
    // 设置毕业日期
    $("#gradDatePicker").datetimepicker({
        format: "YYYY-MM-DD",
        locale: moment.locale(__lang),
        showTodayButton:true,
        showClear:true
    });
    // 设置毕业日期
    $("#zzDatePicker").datetimepicker({
        format: "YYYY-MM-DD",
        locale: moment.locale(__lang),
        showTodayButton:true,
        showClear:true
    });

    // 快速清除当前文本框（disabled文本框除外）
    $(".clear-data").click(function(){
        if (!$(this).prev().attr("value")) {
            $(this).prev().val("").focus();
        }
    });


    /**
     * 给所有的文本输入框设置默认值
     */
    function initText () {
        // 查询后台用户登录信息
        $.myajax({data:{}, url:"/zz/applyquery", callback:function(response){
            if(response.code == 0){
                $("#staffName").val(response.data.name);
                $("#department").val(response.data.department);
                $("#position").val(response.data.position);
                
                // 填表日期
                $("#zz_form_date").text(response.data.formDate);
            }
        }});
    };

    /**
     * 提交审核
     */
    $("#submitBtn").click(function(){
		PNotify.removeAll();// 移除提示框
        if(checkout1.validate()){
            // 校验成功
            PNotify.removeAll();// 移除提示框
            
            // 准备数据
            var dataParam = {};
            $("*[mkey]").each(function(){
                dataParam[$(this).attr('mkey')] = $(this).val();
            });
            
            $.myajax({data:dataParam, url:"/zz/saveandsubmit", callback:function(response){
                if(parseResult(response)){
                	// 提示成功
    				$.modalTips({context:$.i18n.prop("operation_success"), isShowCloseBtn:false, type:"success", buttons:[{
    					text: $.i18n.prop("confirm"),
    					callfunc: function(){
    						window.location.href = "zzlist.html";
    					}
    				}]
    				});
                }
            }});
        }else{
            // 校驗失敗，提示
            new PNotify({
                title: $.i18n.prop('check_failed_title'),
                text: $.i18n.prop('check_failed_text'),
                type: 'error',
                styling: 'brighttheme'
            });
        }
    });

    /**
     * 保存草稿
     */
    $("#saveBtn").click(function(){
        // 准备数据
        var dataParam = {};
        $("*[mkey]").each(function(){
            dataParam[$(this).attr('mkey')] = $(this).val();
        });
        
        $.myajax({data:dataParam, url:"/zz/savedraft", callback:function(response){
            if(parseResult(response)){
                // 提示成功
                $.modalTips({context:$.i18n.prop("operation_success"), isShowCloseBtn:false, type:"success", buttons:[{
                    text: $.i18n.prop("confirm"),
                    callfunc: function(){
                        window.location.href = "zzlist.html";
                    }
                }]
                });
            }
        }});
    });

    /**
     * 取消申请
     */
    $("#backBtn").click(function(){
        history.back();
    });
});